package com.acegear.horizon.domain.repository;

import com.acegear.horizon.domain.models.User;
import com.acegear.horizon.domain.repository.jpa.PasswordAuthRepository;
import com.acegear.horizon.domain.repository.jpa.SecureTokenRepository;
import com.acegear.horizon.domain.repository.jpa.UserAuthRepository;
import com.acegear.horizon.domain.repository.jpa.UserEmailRepository;
import com.acegear.horizon.domain.repository.jpa.UserPhoneRepository;
import com.acegear.horizon.domain.repository.jpa.WechatAuthRepository;
import com.acegear.horizon.domain.repository.jpa.WeiboAuthRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Created by wangsike on 2016/12/17.
 */
@Component
public class UserRepository {

    @Autowired
    private UserAuthRepository userAuthRepository;


    @Autowired
    private WechatAuthRepository wechatAuthRepository;

    @Autowired
    private WeiboAuthRepository weiboAuthRepository;

    @Autowired
    private PasswordAuthRepository passwordAuthRepository;

    @Autowired
    private UserEmailRepository userEmailRepository;

    @Autowired
    private UserPhoneRepository userPhoneRepository;

    @Autowired
    private SecureTokenRepository secureTokenRepository;




    public Optional<User> findByUsername(String username) {
        return passwordAuthRepository
                .findByUsername(username)
                .flatMap(passwordAuth -> findByUserId(passwordAuth.getUserId()));
    }

    public Optional<User> findByPhone(String phone) {
        return userPhoneRepository
                .findByPhone(phone)
                .flatMap(userPhone -> findByUserId(userPhone.getUserId()));
    }

    public Optional<User> findByEmail(String email) {
        return userEmailRepository
                .findByEmail(email)
                .flatMap(userEmail -> findByUserId(userEmail.getUserId()));
    }

    public Optional<User> findByUid(String uid) {
        return weiboAuthRepository
                .findByUid(uid)
                .flatMap(weiboAuth -> findByUserId(weiboAuth.getUserId()));
    }

    public Optional<User> findByUnionIdAndType(String unionId, Integer type) {
        return wechatAuthRepository
                .findByUnionIdAndType(unionId, type)
                .flatMap(wechatAuth -> findByUserId(wechatAuth.getUserId()));
    }

    public Optional<User> findByUnionId(String unionId) {
        return wechatAuthRepository
                .findTopByUnionId(unionId)
                .flatMap(wechatAuth -> findByUserId(wechatAuth.getUserId()));
    }

    public Optional<User> findByOpenId(String openId) {
        return wechatAuthRepository
                .findByOpenId(openId)
                .flatMap(wechatAuth -> findByUserId(wechatAuth.getUserId()));
    }

    public Optional<User> findByUserId(Long userId) {
        return userAuthRepository.findByUserId(userId).map(User::new);
    }


    public Optional<User> findBySecureToken(String token) {
        return secureTokenRepository
                .findByToken(token)
                .flatMap(secureToken ->
                        secureToken.getPhone() == null ?
                                findByEmail(secureToken.getEmail()) :
                                findByPhone(secureToken.getPhone()));
    }


}
